/*
 * @Author: 13938960424 1758142861@qq.com
 * @Date: 2023-11-09 14:58:06
 * @LastEditors: 13938960424 1758142861@qq.com
 * @LastEditTime: 2023-11-09 18:50:52
 * @FilePath: /sjf/new-xshell/muduo/test/http_test/client.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include <iostream>
#include "../../server/muduo_server.hpp"

/**
 * 长连接测试
*/
void Alive_test()
{
    Socket cli_socket;
    cli_socket.CreateClient("127.0.0.1",8085);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    while(1)
    {
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_socket.Recv(buf,1023));
        DBG_LOG("recv:[%s]",buf);
        sleep(3);
    }
    cli_socket.Close();
}

/**
 * 超市连接测试
*/
void close_live_test()
{
    Socket cli_socket;
    cli_socket.CreateClient("127.0.0.1",8085);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    while(1)
    {
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_socket.Recv(buf,1023));
        DBG_LOG("recv:[%s]",buf);
        sleep(15);
    }
    cli_socket.Close();
}
/*
    给服务器发送一个数据，告诉服务器要发送1024字节的数据，但是实际发送的数据不足1024，查看服务器处理结果
    也就是发送数据不足的情况
*/
/*
    1. 如果数据只发送一次，服务器将得不到完整请求，就不会进行业务处理，客户端也就得不到响应，最终超时关闭连接
    2. 连着给服务器发送了多次 小的请求，  服务器会将后边的请求当作前边请求的正文进行处理，而后便处理的时候有可能就会因为处理错误而关闭连接
*/
void Over_test()
{
    Socket cli_socket;
    cli_socket.CreateClient("127.0.0.1",8085);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    while(1)
    { 
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_socket.Recv(buf,1023));
        DBG_LOG("recv:[%s]",buf);
        sleep(3);
    }
    cli_socket.Close();
}

/**
 * 业务处理超时后,查看服务器的处理情况 
 * 当服务器达到了性能的瓶颈时，在一个业务中会花费很长的时间进行处理,此时会超过我们的活跃度
 * 假设现在有12345个描述符就绪，在1号描述符时花费了30s处理，超时了，那么2345号就会因为长时间没有刷新活跃度而被释放
 * 1.如果2345号描述符都是通信连接描述符，都就绪了，那么并不会受到影响，因为接下来就会刷新活跃度
 * 2.如果接下来的2号描述符是一个定时任务，定时器触发了，下面的345号就会被释放
 * 所以在处理2号任务的时候，不能直接对后序任务一同释放，而是也要把释放操作压入到任务池中
 * 等待事件处理完成之后再进行释放操作
*/
bool business_test()
{
    signal(SIGCHLD, SIG_IGN);
    for (int i = 0; i < 10; i++) {
        pid_t pid = fork();
        if (pid < 0) {
            DBG_LOG("FORK ERROR");
            return -1;
        }else if (pid == 0) {
            Socket cli_sock;
            cli_sock.CreateClient("127.0.0.1",8085);
            std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
            while(1) {
                assert(cli_sock.Send(req.c_str(), req.size()) != -1);
                char buf[1024] = {0};
                assert(cli_sock.Recv(buf, 1023));
                DBG_LOG("[%s]", buf);
            }
            cli_sock.Close();
            exit(0);
        }
    }
    while(1) sleep(1);
    return true;
}
/**
 * 一次发送多条数据
*/
void many_test()
{
     Socket cli_socket;
    cli_socket.CreateClient("127.0.0.1",8085);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";;
    while(1)
    { 
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_socket.Recv(buf,1023));
        DBG_LOG("recv:[%s]",buf);
        sleep(3);
    }
    cli_socket.Close();
}
/**
 * 大文件传输处理
 * 创建一个1g的文件 dd if=/dev/zero of=./hello.text bs=1G count=1
*/
int main()
{
    Socket cli_socket;
    cli_socket.CreateClient("127.0.0.1",8085);
    std::string req = "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";
    req += "GET /hello HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n";;
    while(1)
    { 
        assert(cli_socket.Send(req.c_str(),req.size()) != -1);
        char buf[1024] = {0};
        assert(cli_socket.Recv(buf,1023));
        DBG_LOG("recv:[%s]",buf);
        sleep(3);
    }
    cli_socket.Close();
    return 0;
}